package com.thankgod7.jobcenter.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.thankgod7.jobcenter.entity.HisPatient;
import com.thankgod7.jobcenter.service.IHisPatientService;
import com.thankgod7.jobcenter.service.IJobDataPushLogService;
import com.thankgod7.jobcenter.utils.RedisUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 同步患者基础信息
 *
 * @author ouyangdong@talkweb.com.cn
 * @date 2024/11/12 10:59
 * @apiNote
 */
@Slf4j
@Component
@SuppressWarnings("unused")
public class SyncPatientJob {

    /**
     * 患者缓存键值
     */
    private static final String SYNC_PATIENT_KEY_PREFIX = "sync::cache::patient::";

    /**
     * 患者数据推送类型
     */
    private static final String SYNC_PATIENT_TYPE = "PATIENT";

    /**
     * 更新患者范围
     */
    private static final Integer SYNC_PATIENT_SPLIT = 15;

    @Resource
    RedisUtil redisUtil;

    @Resource
    IHisPatientService hisPatientService;

    @Resource
    IJobDataPushLogService jobDataPushLogService;

    @Value("${push.patient}")
    private String pushUrls;

    /**
     * 同步患者基础信息
     *
     * @return ret
     */
    @XxlJob(value = "sync_patient")
    public ReturnT<String> execute(String param) {
        log.info("同步患者基础信息任务开始执行....");
        ReturnT<String> t = new ReturnT<>();
        //查询患者基本信息
        try {
            //查询his最近15天患者数据
            String dayOffset = DateUtil.format(DateUtil.offsetDay(new Date(), -SYNC_PATIENT_SPLIT), "yyyyMMdd");
            List<HisPatient> hisPatientList = hisPatientService.list(new LambdaQueryWrapper<HisPatient>().ge(HisPatient::getDjrq, dayOffset));
            List<HisPatient> updatePatientList = new ArrayList<>();
            for (HisPatient hisPatient : hisPatientList) {
                //判断是否需要推送数据变更消息
                if (!redisUtil.hasKey(hisPatient.getHzsfz()) || Objects.equals(redisUtil.get(hisPatient.getHzsfz()), hisPatient.hashCode())) {
                    //数据变更-》推送消息 ->>>>>>>>>>>>>>>>>>>单个推送样例
                    //jobDataPushLogService.push(SYNC_PATIENT_KEY_PREFIX + hisPatient.getHzsfz(),
                    //        hisPatient.hashCode(),
                    //        SYNC_PATIENT_SPLIT * 60 * 60 * 24L,
                    //        pushUrls,
                    //        SYNC_PATIENT_TYPE, JSON.toJSONString(hisPatient));
                    //数据变更-》推送消息 ->>>>>>>>>>>>>>>>>>>批量推送样例
                    updatePatientList.add(hisPatient);
                }
            }
            jobDataPushLogService.pushBatch(SYNC_PATIENT_KEY_PREFIX,
                    "hzsfz",
                    SYNC_PATIENT_SPLIT * 60 * 60 * 24L,
                    pushUrls,
                    SYNC_PATIENT_TYPE,
                    updatePatientList);
            t.setCode(ReturnT.SUCCESS_CODE);
            t.setMsg("同步患者基础信息任务执行成功");
        } catch (Exception e) {
            e.printStackTrace();
            t.setCode(ReturnT.FAIL_CODE);
            t.setMsg("同步患者基础信息任务执行失败," + e.getMessage());
        }
        log.info("同步患者基础信息任务执行结束....");
        return t;
    }


}
